﻿@model Sohobloo.SoAuth.Web.Models.PermissionModel

<h2>Permission</h2>
@Html.ActionLink("Back", "Index", "Home")
@{
    <div>
        <div style="display: inline">
            @Html.DropDownList("RoleList", new SelectList(Model.RoleList, "Id", "RoleName"), "Please select...")
        </div>
        <div style="display: inline">
            @Html.DropDownList("FunctionList", new SelectList(Model.RoleFunctionList, "Id", "FunctionName"), "Please select...")
        </div>
        <div id="uActionList" style="display: inline">
        </div>
        <a id="Add" href="javascript:void(0);" style="display: none;">Add</a>
    </div>
    <div id="uPermissionList">
    </div>
}

@section scripts
{
    @Scripts.Render("~/bundles/jqueryui")
    <script>
        $().ready(function() {
            var $RoleList = $("#RoleList");
            var $FunctionList = $("#FunctionList");
            var $uActionList = $("#uActionList");
            //var $ActionList = $("#ActionList");
            var $Add = $('#Add');
            var $uPermissionList = $("#uPermissionList");

            var roleId = $RoleList.val();
            if (!roleId) {
                $FunctionList.hide();
            }

            $RoleList.change(function() {
                roleId = $RoleList.val();
                if (!roleId) {
                    $FunctionList.hide();
                } else {
                    var oldFunctionId = $FunctionList.val();
                    if (oldFunctionId != "") {
                        $FunctionList.val("");
                        $FunctionList.trigger("change");
                    }
                    $FunctionList.show();
                    $uPermissionList.load("/Permission/GetPermissions",
                        {
                            roleId: roleId
                        });
                }
            });

            $FunctionList.change(function() {
                var functionId = $FunctionList.val();
                if (!functionId || functionId == "") {
                    $uActionList.html("");
                    $Add.hide();
                } else {
                    roleId = $RoleList.val();
                    $uActionList.load("/Permission/GetActionList",
                        {
                            roleId: roleId,
                            functionId: functionId
                        });
                }
            });

            $("#ActionList").live("change", function () {
                var $self = $(this);
                var actionId = $self.val();
                if (!actionId || actionId == "") {
                    $Add.hide();
                } else {
                    $Add.show();
                    roleId = $RoleList.val();
                }
            });

            $Add.click(function () {
                roleId = $RoleList.val();
                var actionId = $("#ActionList").val();
                $.ajax(
                    {
                        url: "/Permission/Add",  //要查询的Action
                        type: "POST",  //Post提交
                        dataType: "json", //json格式数据，默认是text
                        data: {
                            roleId: roleId,
                            actionId: actionId
                        }, //参数
                        success: function () {
                            var functionId = $FunctionList.val();
                            $uActionList.load("/Permission/GetActionList",
                                {
                                    roleId: roleId,
                                    functionId: functionId
                                });
                            $Add.hide();
                            $uPermissionList.load("/Permission/GetPermissions",
                                {
                                    roleId: roleId
                                });
                        }
                    }
                );
            });
        });
    </script>
}